import { Observable,of } from "rxjs";
import HttpClient from "./httpClient"; 
import { resultDto } from "./dto/resultDto";
import { Axios, AxiosProgressEvent } from "axios";
  
export class fileTransferRequest{
 
   private hc = new HttpClient("/api/fileTransfer");

   public fileUpload(path: string,file: File,onUploadProgress?: (progressEvent:AxiosProgressEvent)=>void) : Observable<resultDto<null>> {
        const formData = new FormData();
        formData.append('path', path);
        formData.append('file', file); 
        return this.hc.post<resultDto<null>>(
          "/fileUpload", 
          formData, 
          { 
               headers: {'Content-Type': 'multipart/form-data'},
               onUploadProgress: onUploadProgress
          });
   } 


   public bigFileUploadInit(size : number) : Observable<resultDto<{key: string, chunkCount: number, chunkSize: number}>> { 
        return this.hc.post<resultDto<{key: string, chunkCount: number, chunkSize: number}>>(
         `/bigFileUpload/init`, {size}, {headers: {'Content-Type': 'application/json'}}); 
   }

   public bigFileUploadChunk(key: string, index: number, chunk: File|Blob) : Observable<resultDto<null>> {
        const formData = new FormData();
        formData.append('key', key);
        formData.append('index', index.toString());
        formData.append('chunk', chunk);
        return this.hc.post<resultDto<null>>("/bigFileUpload/chunk", formData, {headers: {'Content-Type': 'multipart/form-data'}});
   }

   public bigFileUploadComplete(key:string,name:string,size:number,chunkSize:number,chunkCount:number,path:string,repeatPolicy:number) : Observable<resultDto<string>> {
       
        return this.hc.post<resultDto<string>>(
         `/bigFileUpload/complete`, {key,name,size,chunkSize,chunkCount,path,repeatPolicy}, {headers: {'Content-Type': 'application/json'}});
   }

   public bigFileUploadCancel(key:string) : Observable<resultDto<null>> {
        return this.hc.post<resultDto<null>>(
         `/bigFileUpload/cancel`, {key}, {headers: {'Content-Type': 'application/json'}});
   } 
}
 